package com.cv4j.app.fragment;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.Rect;
import com.cv4j.image.util.QRCodeScanner;

/**
 * QRFragment
 *
 * @since 2021-05-18
 */
public class QRFragment {
    private Context mContext;
    private int mIndex;
    private PixelMap pixelMap;
    private Component component;
    private Image image;
    private Button detectbtn;

    private QRFragment(Context context, int index) {
        mContext = context;
        mIndex = index;
        init();
    }

    /**
     * 重写界面
     *
     * @param context 上下文
     * @param index 下标
     * @return qrFragment
     */
    public static QRFragment getInstance(Context context, int index) {
        QRFragment qrFragment = new QRFragment(context, index);
        return qrFragment;
    }

    private void init() {
        component = LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_fragment_qr, null, false);
        component.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_PARENT));
        image = (Image) component.findComponentById(ResourceTable.Id_image);
        detectbtn = (Button) component.findComponentById(ResourceTable.Id_detect_btn);

        detectbtn.setClickedListener((Component component) -> {
            CV4JImage cv4jimage = new CV4JImage(pixelMap);
            QRCodeScanner qrCodeScanner = new QRCodeScanner();
            final Rect rect = qrCodeScanner.findQRCodeBounding(cv4jimage.getProcessor(), 1, 6);
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.size = pixelMap.getImageInfo().size;
            PixelMap bm = PixelMap.create(pixelMap, options);
            final Canvas canvas = new Canvas(new Texture(bm));
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            final float width = 10.0f;
            paint.setStrokeWidth(width);
            paint.setStyle(Paint.Style.STROKE_STYLE);
            final int count = 20;
            ohos.agp.utils.Rect ohosRect = new ohos.agp.utils
                .Rect(rect.x1 - count, rect.y1 - count, rect.br().x1 + count, rect.br().y1 + count);
            canvas.drawRect(ohosRect, paint);
            image.setPixelMap(bm);
        });
        initData();
    }

    private void initData() {
        final int two = 2;
        final int three = 3;
        switch (mIndex) {
            case 0:
                pixelMap = PixelMapUtil.getPixelMapFromResource(mContext, ResourceTable.Media_qr_1).get();
                break;
            case 1:
                pixelMap = PixelMapUtil.getPixelMapFromResource(mContext, ResourceTable.Media_qr_applepay).get();
                break;
            case two:
                pixelMap = PixelMapUtil.getPixelMapFromResource(mContext, ResourceTable.Media_qr_jiazhigang).get();
                break;
            case three:
                pixelMap = PixelMapUtil.getPixelMapFromResource(mContext, ResourceTable.Media_qr_tony).get();
                break;
            default:
                pixelMap = PixelMapUtil.getPixelMapFromResource(mContext, ResourceTable.Media_qr_1).get();
                break;
        }
        image.setPixelMap(pixelMap);
    }

    public Component getComponent() {
        return component;
    }
}
